package com.mida.app.report.service.impl;

import com.mida.app.report.service.DetailCodingSystemService;
import com.mida.app.report.service.DocStructTemplateInfoService;
import com.mida.app.report.service.MdmService;
import com.mida.bms.report.api.DocStructTemplateInfoApi;
import com.mida.bms.report.entity.DocStructTemplateInfo;
import com.mida.bms.report.vo.DocStructTemplateInfoVo;
import com.mida.bms.report.vo.PageResultVo;
import com.mida.dms.mdm.dto.UserDto;
import com.mida.dms.mdm.vo.PullDownMenuVO;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.BaseResult;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 模版基本信息的业务逻辑实现层
 *
 * @author dj
 */
@Service
@Slf4j
@Transactional
public class DocStructTemplateInfoServiceImpl extends
        BaseResult implements DocStructTemplateInfoService {

    private static final String CURRENT_ROLE_ID_CT_WRITE = "36176074815569920";
    private static final String CURRENT_ROLE_ID_CT_CHECK = "40522790851313664";
    private static final String CURRENT_ROLE_ID_US_WRITE = "41263060182106112";
    private static final long CT_TYPE = 1983l;
    private static final long US_TYPE = 1986l;
    private static final String DHUCVDT_0021 = "DHUCVDT0021";
    private static final String DHUCVKNW_0029 = "DHUCVKNW0029";

    /**
     * 模版基本信息API
     */
    @Autowired
    private DocStructTemplateInfoApi docStructTemplateInfoApi;

    @Autowired
    private DetailCodingSystemService detailCodingSystemService;

    @Autowired
    private MdmService mdmService;

    /**
     * 添加模版基本信息
     *
     * @param docStructTemplateInfoVo
     * @return
     * @throws Exception
     */
    @Override
    public ActionResult addDocStructTemplateInfo(
            DocStructTemplateInfoVo docStructTemplateInfoVo) throws Exception {
        return docStructTemplateInfoApi.addDocStructTemplateInfo(docStructTemplateInfoVo);
    }

    /**
     * 更新模版基本信息
     *
     * @param docStructTemplateInfoVo
     * @return
     * @throws Exception
     */
    @Override
    public ActionResult updateDocStructTemplateInfo(
            DocStructTemplateInfoVo docStructTemplateInfoVo) throws Exception {
        return docStructTemplateInfoApi.updateDocStructTemplateInfo(docStructTemplateInfoVo);
    }

    /**
     * 逻辑删除模版基本信息
     *
     * @param stTemplateInfoSoid
     * @return
     * @throws Exception
     */
    @Override
    public ActionResult deleteDocStructTemplateInfo(Long stTemplateInfoSoid) throws Exception {
        return docStructTemplateInfoApi.deleteDocStructTemplateInfo(stTemplateInfoSoid);
    }

    /**
     * 查询模板分页列表处理
     * @param docStructTemplateInfoVo
     * @return
     * @throws Exception
     */
    @Override
    public ListResult<PageResultVo<DocStructTemplateInfo>> getDocStructTemplateInfoPage(
            DocStructTemplateInfoVo docStructTemplateInfoVo) throws Exception {

        // 根据根据登录用户角色对应的检查类型、获取对应的模板分页列表
        ListResult<PageResultVo<DocStructTemplateInfo>> list =
                findDocStructTemplateInfoPage(docStructTemplateInfoVo);

        // 设置模板信息相关属性名称
        setDocStructTemplateInfoPropertyName(list);

        return list;
    }

    /**
     * 设置模板信息相关属性名称
     * @param list
     * @throws Exception
     */
    private void setDocStructTemplateInfoPropertyName(ListResult<PageResultVo<DocStructTemplateInfo>> list) throws Exception {
        if (list != null && list.getData() != null) {
            PageResultVo<DocStructTemplateInfo> pageResultVoTmp = list.getData();
            List<DocStructTemplateInfo> listTmp = pageResultVoTmp.getList();
            if (listTmp != null && listTmp.size() > 0) {
                List<PullDownMenuVO> listExamServiceType
                        = detailCodingSystemService.drawer(DHUCVDT_0021, null, false);
                List<PullDownMenuVO> listStatus
                        = detailCodingSystemService.drawer(DHUCVKNW_0029, null, false);

                Map<Long, String> map = new HashMap<>();
                for (PullDownMenuVO pv : listExamServiceType) {
                    if (map.get(Long.valueOf(pv.getCode())) == null) {
                        map.put(Long.valueOf(pv.getCode()), pv.getValue());
                    }
                }
                for (PullDownMenuVO pv : listStatus) {
                    if (map.get(Long.valueOf(pv.getCode())) == null) {
                        map.put(Long.valueOf(pv.getCode()), pv.getValue());
                    }
                }

                for (DocStructTemplateInfo d : listTmp) {
                    if (map.get(d.getExamServiceType()) != null) {
                        d.setExamServiceTypeName(map.get(d.getExamServiceType()));
                    }

                    if (map.get(d.getStatus()) != null) {
                        d.setStatusName(map.get(d.getStatus()));
                    }
                }

                pageResultVoTmp.setList(listTmp);
            }
        }
    }

    /**
     * 根据根据登录用户角色对应的检查类型、获取对应的模板分页列表
     * @param docStructTemplateInfoVo
     * @return
     */
    private ListResult<PageResultVo<DocStructTemplateInfo>> findDocStructTemplateInfoPage(
            DocStructTemplateInfoVo docStructTemplateInfoVo) throws Exception {

        ListResult<UserDto> userDtoResult = mdmService.getLoginUserInfo();

        List<Long> longList = new ArrayList<>();
        if (userDtoResult != null && userDtoResult.getData() != null) {
            UserDto u = userDtoResult.getData();
            if (u.getCurrentRole() != null && u.getCurrentRoleId() != null) {
                if (CURRENT_ROLE_ID_CT_WRITE.equals(u.getCurrentRoleId().toString()) ||
                        CURRENT_ROLE_ID_CT_CHECK.equals(u.getCurrentRoleId().toString())) {
                    longList.add(CT_TYPE);
                }

                if (CURRENT_ROLE_ID_US_WRITE.equals(u.getCurrentRoleId().toString())) {
                    longList.add(US_TYPE);
                }
                docStructTemplateInfoVo.setListExamServiceTypeSoid(longList);
            }
        }

        return docStructTemplateInfoApi.getDocStructTemplateInfoPage(docStructTemplateInfoVo);
    }

    /**
     * 根据模板soid查询模版数据
     *
     * @param stTemplateInfoSoid
     * @return
     * @throws Exception
     */
    @Override
    public ListResult getDocStructTemplateInfo(Long stTemplateInfoSoid) throws Exception {
        return docStructTemplateInfoApi.getDocStructTemplateInfo(stTemplateInfoSoid);
    }

}
